package com.school.aftersalemanagement.service;

import android.content.Context;
import android.os.Handler;
import android.util.Log;

import com.school.aftersalemanagement.global.BaseActivity;
import com.school.aftersalemanagement.global.UserConfig;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.request.RequestCall;


import java.util.HashMap;
import java.util.Map;

/**
 * Created by winkyle on 2017/11/8.
 */

public abstract class Service {
    protected Map<String,String> map = new HashMap<String,String>();
    protected Context context;
    protected Handler handler;
    protected String cookie;

    public Service(Context context, Handler handler) {
        this.context = context;
        this.handler = handler;
    }

    protected RequestCall getRequestCall(String url, Map<String,String> map) {
        cookie = UserConfig.getInstance(context).getCookie();
        Log.d("cookieget",cookie);
        RequestCall requestCall;
        requestCall = OkHttpUtils.get()
                     .url(url)
                     .addHeader("cookie", cookie)
                     .params(map)
                     .build();
        return requestCall;
    }

    protected RequestCall postRequestCall(String url, Map<String,String> map) {
        cookie = UserConfig.getInstance(context).getCookie();
        Log.d("cookieget",cookie);
        RequestCall requestCall;
        requestCall = OkHttpUtils.post()
                      .url(url)
                      .params(map)
                      .build();
        return requestCall;
    }

    public void showShort(final String msg) {
        ((BaseActivity)context).showShort(msg);
    }

    public boolean isLogin() {
        cookie = UserConfig.getInstance(context).getCookie();
        if ("".equals(cookie)) {
            showShort("未登录，请登录");
            return false;
        } else {
            return true;
        }
    }
}
